<?php

class UserController extends Zend_Controller_Action
{

	/**
	 * indexAction
	 * redirect to listAction to show all use
	 */
	public function indexAction()
	{
		$mapper = new Application_Model_Usermapper(); //open db communication on user table
		 
		$this->view->entries = $mapper->fetchAll(); //get all user in an array and forward to view
		$this->view->pageTitle = "List all user";	//set page Title
	}
	/**
	 * addAction
	 * add a new user
	 */
	public function addAction()
	{

		$request = $this->getRequest();
		$form = new Application_Form_UserAdd();
		$mapper = new Application_Model_UserMapper();

		if($request->isPost()) {
			if ($form->isValid($request->getPost())) {
				// fill form data into user model
				$user = new Application_Model_User();

				$user->setName($form->getValue('name'));
				$user->setSurname($form->getValue('surname'));
				$user->setUsername($form->getValue('username'));
				$user->setPassword(md5($form->getValue('password')));
				$user->setEmail($form->getValue('email'));
				$user->setUserrole($form->getValue('userrole'));

				$mapper->add($user);

				$this->_redirect('user/index');
			}

		}
		$this->view->pageTitle = "Add new user";
		$this->view->form = $form;

	}
	
	/**
	 * editUser
	 * changes user data
	 */
	public function editAction()
	{
		$request = $this->getRequest();
		$form = new Application_Form_UserEdit();
		$mapper = new Application_Model_UserMapper();
		 
		if($request->isPost()) {
			if ($form->isValid($request->getPost())) {
				// fill form data into user model
				$usermodel = new Application_Model_User();
				 
				$usermodel->setName($form->getValue('name'));
				$usermodel->setSurname($form->getValue('surname'));
				$usermodel->setUsername($form->getValue('username'));
				$usermodel->setEmail($form->getValue('email'));
				$usermodel->setUserrole($form->getValue('userrole'));
				$usermodel->setId($form->getValue('id'));
				 
				
				$mapper->edit($usermodel);
				 
				$this->_redirect('user/index');
			}
			 
		} else {
			
			$param = $request->getParams(); //get UserID from URL

			if ($param['userid'] == null)
			{
				die;
			}
			else {
				$user = $mapper->find($param['userid']); //get user id
				$form->populate($user);	//fill form with userdata
			}
		}
		 
		$this->view->pageTitle = 'Edit user';
		$this->view->form = $form;	//show form
		 
	}
	
	public function accountinfoAction()
	{
		/**
		 * Get user details
		 */
		$userId = Zend_Auth::getInstance()->getStorage()->read()->id;
			
		$userMapper = new Application_Model_UserMapper();
			
		$userData = $userMapper->find($userId);
		$this->view->userdata = $userData;
			
			
		/**
		 *	Form for changing password
		 */
		$form = new Application_Form_AuthenticationChangePassword();
	
		$request = $this->getRequest();
	
		if($request->isPost())
		{
			if ($form->isValid($request->getPost()))
			{
				$userMapper->updatePassword($request->getParams());
	
				$this->view->pageTitle = 'Password has been changed!';
	
			}
	
		}
	
		$form->getElement('id')->setValue($userId);
	
	
		$this->view->form = $form;
	}


}